from flask import Flask,render_template,request
from pachong import qiluSpider,qianChengSpider,spiderliepin
from database import operation
import util
import json

app=Flask(__name__)

@app.route('/')
def main():
    return render_template('index.html')



#结果显示界面：
@app.route('/spider',methods=['GET','POST'])
def resultShow():
    global ls
    #获取表单数据requests.form
    parm = request.form
    code=request.args.get('code')
    #获得数据库链接
    DB=operation.DateProcess()
    connection = DB.connect()

    #由主页进入结果显示界面
    if code=='1':
        # 从爬虫首页跳转
        pageNumber = int(parm.get("total"))
        total = parm.get('total')
        hour = parm.get('hour')
        minute = parm.get('minute')
        mutiplyThread = parm.get('mutiplyThread')
        threads=[]
        DB.create(connection);
        #前程无忧网
        connection.ping(True)
        if parm.get('web1')!=None:
            qianCheng=qianChengSpider.qiancheng_wuyouSpider(pageNumber,connection)
           # qianCheng.start()
            threads.append(qianCheng)
            #qianCheng.join()
        #齐鲁人才网
        if parm.get('web2')!=None:
            qiLu=qiluSpider.qiLuSpider(conn=connection,pageNumber=pageNumber)
            #qiLu.start()
            threads.append(qiLu)
            #qiLu.join()
        #百聘网
        if parm.get('web3')!=None:
            liePin=spiderliepin.SpiderLiepin(pageNumber, connection)
            threads.append(liePin)
            #liePin.join()
        for i in threads:
            i.start()
            i.join()
        ls = DB.selectAll(connection)
    elif code=='2':
        education=parm.get('education')
        experience = parm.get('experience')
        salary = parm.get('salary')
        ls=DB.AllSelect(education,experience,salary,connection)
        #结果显示的‘主页’
    elif code=='3':
        ls=DB.selectAll(connection)
    elif code=='4':
        name=parm.get("name")
        ls=DB.selectByName(conn=connection,name=name)
    return render_template('showResult.html',ls=ls)
# 薪水与工作的关系
@app.route('/test2')
def salary2():
    salary_dict = util.salaryJob()
    salary_dict_key = list(salary_dict.keys())
    salary_dict_value = list(salary_dict.values())
    return json.dumps({'key': salary_dict_key, 'value': salary_dict_value}, ensure_ascii=False)

# 工作的最低和最高薪水
@app.route('/test3')
def salary_min_max():
    salary_min_max = util.MinSalary()
    salary_min_max_key = list(salary_min_max.keys())
    salary_max_min_values = list(salary_min_max.values())
    return json.dumps({'key': salary_min_max_key, 'value': salary_max_min_values}, ensure_ascii=False)

# 从事计算机行业的学历统计
@app.route('/test4')
def numEducation():
    numEducation_data = util.numEducation()

    return json.dumps(numEducation_data, ensure_ascii=False)

# 学历与薪水的关系
@app.route('/salaryEducation')
def salaryEducation():
    salaryEducation_data = util.salaryEducation()
    salaryEducation_data_key = list(salaryEducation_data.keys())
    salaryEducation_data_value = list(salaryEducation_data.values())
    return json.dumps({'key': salaryEducation_data_key, 'value': salaryEducation_data_value}, ensure_ascii=False)




@app.route('/chart')
def hello_world():  # put application's code here
    return render_template('menu.html')

@app.errorhandler(404)
def _404(e):
    return render_template('404.html')

if __name__=='__main__':
    app.run(debug=True)



